package com.jdy.hotel.check;

import android.text.TextUtils;
import android.util.Patterns;

import com.jdy.hotel.R;
import com.jdy.hotel.utils.ResourcesUtils;

public class BaseCheck implements Check {

    @Override
    public String checkResult(String value) {
        return null;
    }

    /**
     * 检查对象是否为空
     */
    public final static class NullCheck implements Check {
        @Override
        public String checkResult(String value) {
            if (TextUtils.isEmpty(value)) {
                return ResourcesUtils.getString(R.string.calibrator_not_null);
            }
            return null;
        }
    }

    /**
     * 检查对象是否为手机号码
     */
    public final static class PhoneCheck implements Check {
        @Override
        public String checkResult(String value) {
            if (Patterns.PHONE.matcher(value).matches()) {
                return null;
            }
            return ResourcesUtils.getString(R.string.calibrator_email_error);
        }
    }

    /**
     * 检查对象是否为Email
     */
    public final static class EmailCheck implements Check {
        @Override
        public String checkResult(String value) {
            if (Patterns.EMAIL_ADDRESS.matcher(value).matches()) {
                return null;
            }
            return ResourcesUtils.getString(R.string.calibrator_email_error);
        }
    }

    /**
     * 检查对象是否为Email
     */
    public final static class EqualCheck implements Check {

        private CheckItem targetItem;

        public void setTargetItem(CheckItem targetItem) {
            this.targetItem = targetItem;
        }

        @Override
        public String checkResult(String value) {
            if (targetItem == null) {
                return ResourcesUtils.getString(R.string.calibrator_error);
            }

            String targetValue = targetItem.getValue();
            if (TextUtils.isEmpty(targetValue))
                return ResourcesUtils.getString(R.string.calibrator_error);

            if (targetValue.equals(value))
                return null;

            return String.format(ResourcesUtils.getString(R.string.calibrator_equal_error),targetItem.getPromptName());
        }
    }
}
